//
//  Frases_D_FutbolViewController.m
//  Frases D Futbol
//
//  Created by Jose Luis Lopez on 22/05/11.
//  Copyright 2011 None. All rights reserved.
//

#import "Frases_D_FutbolViewController.h"
#import "Autor.h"
#import "Autores.h"
#import "Mas.h"

@implementation Frases_D_FutbolViewController

@synthesize listData;

- (void)dealloc
{
    [super dealloc];
}

#pragma mark - View lifecycle

-(id)init {
    NSLog(@"En el init");
    return self = [super init];
}

- (void)viewDidLoad
{
    self.screenName = @"Main Screen";
    
    
    [super viewDidLoad];
    
    
    UIButton *masButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [masButton addTarget:self action:@selector(mas:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:masButton];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

    
    self.title = @"Autores";
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain];
    [tableView setDataSource:self];
    [tableView setDelegate:self];
    [self.view addSubview:tableView];
    
    NSMutableArray *aux = [NSMutableArray array];
    Autores *auts = [[Autores alloc] init];
    for (Autor *aut in auts.listaAutores) {
        [aux addObject:aut.nombreAutor];
    }
    
    NSArray *array = [NSArray arrayWithArray:aux];
     
    self.listData = array;

    [super viewDidLoad];
    
    
    NSString *version = [[UIDevice currentDevice] systemVersion];
    int ver = [version intValue];
    if (ver < 7){
        //iOS 6 work
    }
    else{
        //iOS 7 related work
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Table View Data Source Methods

//  El Table View tendrá tantas celdas como elementos nuestro array.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listData count];
}

//  Se encarga de la visualizacion de la tabla.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    
    //  Para no perder los datos que queden fuera de la pantalla.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    //  Se cambia el texto de la label con el texto del array de la posicion correspondiente.
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.listData objectAtIndex:row];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    
    return cell;    
}

//  Aqui se pone el codigo que queremos que se ejecute cuando se toque una celda.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    NSString *cadena = [NSString stringWithFormat:@"%d", row];
    AutoresController * controller = [[AutoresController alloc] initWithNibName:cadena bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//    [self presentModalViewController:controller animated:YES];
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)mas:(id)sender
{
    NSLog(@"mas");
    Mas * controller = [[Mas alloc] initWithNibName:nil bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//    [self presentModalViewController:controller animated:YES];
    
    UINavigationController *navCont = [[UINavigationController alloc] initWithRootViewController:controller];
    [self presentModalViewController:navCont animated:YES];
}

@end
